package com.tjsinfo.tjsoa.mail.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.PageController;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.mail.bo.ITjsEmailFolderBo;
import com.tjsinfo.tjsoa.mail.bo.ITjsFoldMailBo;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmail;
import com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldMail;

/**
 * 查询指定文件夹的邮件
 * @author wangxiaowei
 *
 */
public class SelectFoldEmailAction extends Action{
	private ITjsFoldMailBo tjsFoldMailBo;
	private ITjsEmailFolderBo tjsEmailFolderBo;
	
	public ITjsFoldMailBo getTjsFoldMailBo() {
		return tjsFoldMailBo;
	}
	public void setTjsFoldMailBo(ITjsFoldMailBo tjsFoldMailBo) {
		this.tjsFoldMailBo = tjsFoldMailBo;
	}
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		List<TjsFoldMail> list = tjsFoldMailBo.selectTjsFoldMailByFoldId(request.getParameter("id"));//文件夹邮件
		Set<TjsFoldMail> set = new HashSet<TjsFoldMail>(list);
		list = new ArrayList<TjsFoldMail>(set);
		Comparator comp = new Comparator(){
	          public int compare(Object o1,Object o2) {
	        	  TjsFoldMail p1=(TjsFoldMail)o1;
	        	  TjsFoldMail p2=(TjsFoldMail)o2; 
	            
	        		if (p1.getSortid() < p2.getSortid()) {
	              		return 1;
					} else {
						return 0;
					}
	           
	             }
	        };
	        Collections.sort(list, comp);
		List<TjsEmailFolder> folderlist = tjsEmailFolderBo.selectByUserId(SessionUser.getUserId(request));
		TjsEmailFolder tjsEmailFolder = tjsEmailFolderBo.selectOneId(request.getParameter("id"));
		Integer contentspage = null;
		String start = null;
		 PageController pageController = new PageController();
		    if(request.getParameter("page") != null && request.getParameter("start") == null )
			{
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				list = (ArrayList) pageController.getPageContents(Integer.parseInt(request.getParameter("page") ));
		
			}
		    else if (request.getParameter("page") != null) {
				contentspage = Integer.parseInt(request.getParameter("page"));
				start = request.getParameter("start");
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				if (start.equals("up")) {
					list = (ArrayList) pageController
							.getPageContents(contentspage - 1);
				} else if(start.equals("down")) {
					list = (ArrayList) pageController
							.getPageContents(contentspage + 1);
				}
				else if(start.equals("first"))
				{
					list = (ArrayList) pageController
					.getPageContents(1);
				}
				else if(start.equals("last"))
				{
					list = (ArrayList) pageController
					.getPageContents(pageController.getPageCount());
				}
			} else {
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				list = (ArrayList) pageController.getPageContents(1);
			}
		///request.setAttribute("list", list);
		request.setAttribute("pageController",pageController);
		request.setAttribute("list", list);
		request.setAttribute("list", list);
		request.setAttribute("foldlist", folderlist);
		request.setAttribute("flodid", request.getParameter("id"));
		request.setAttribute("tjsEmailFolder", tjsEmailFolder);
		
		return mapping.findForward("ok");
		
		
	}
	public ITjsEmailFolderBo getTjsEmailFolderBo() {
		return tjsEmailFolderBo;
	}
	public void setTjsEmailFolderBo(ITjsEmailFolderBo tjsEmailFolderBo) {
		this.tjsEmailFolderBo = tjsEmailFolderBo;
	}
	
}
